<?php
/*
	插件页面权限设置，主要为管理端页面
	权限相关功能后期优化
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if(!$_SESSION['plugin_id']){
	exi('本地私域插件，不可操作本页面！','error');
}
$tabs=array(
	array('op'=>'list','title'=>'权限列表'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('plugin_id'=>$_SESSION['plugin_id']);
	$list=pdo_getall('core_plugin_permissions',array('plugin'=>$_SESSION['plugin_name']));
	foreach($list as $key=>$item){
		if(!$item['action'] && !$item['action']){
			unset($list[$key]);
		}
	}
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'list'=>array('pagelist'=>false,'tip'=>'此处仅用作声明插件页面权限等级，设置操作员权限时，系统会自动读取插件权限集供选择','data'=>'list','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'action','title'=>'页面','type'=>'string'),
			array('name'=>'op','title'=>'动作','type'=>'string'),
			array('name'=>'type','title'=>'权限类型','type'=>'radio','options'=>'types'),
		)),
		'search_one'=>array('name'=>'title','help'=>''),
	);
	$return['htmls']=$htmls;
	$return['types']=get_user_types();
	exi($return);
}
if($_RQ['op']=='edit'){
	$params=array('plugin_id'=>$_SESSION['plugin_id']);
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_plugin_permissions',array('id'=>$id));
	}
	$return['item']=$item?:array();
	$return['pages']=cloud_request('plugin.permission.pages',$params);
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edits'=>array(
			array(
				'title'=>'权限设置',
				'list'=>array(
					array('type'=>'string','title'=>'权限名称','name'=>'title'),
					array('type'=>'select','title'=>'权限页面','name'=>'action','options'=>'pages','option_value'=>'name'),
					array('type'=>'select','title'=>'权限op','name'=>'op','options'=>'pages[pagedata.item.action].ops','option_value'=>'name'),
					array('type'=>'radio','title'=>'权限类型','name'=>'type','options'=>'types','help'=>''),
					
				),
			)
		)
	);
	$return['htmls']=$htmls;
	$return['types']=get_user_types();
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array(
		'type'=>intval($post['type']),
		'action'=>trim($post['action']),
		'op'=>trim($post['op']),
		'title'=>trim($post['title']),
	);
	$id=intval($post['id']);
	if($id){
		pdo_update('core_plugin_permissions',$data,array('id'=>$id));
	}else{
		$data['plugin']=$_SESSION['plugin_name'];
		
		pdo_insert('core_plugin_permissions',$data);
	}
	
	exi('设置成功！','','list');
}
if($_RQ['op']=='del'){
	$dels=array();
	if($_RQ['id']){
		$dels[]=intval($_RQ['id']);
	}
	if($_RQ['dels']){
		$_RQ['dels']=str_array($_RQ['dels']);
		foreach($_RQ['dels'] as $id){
			$dels[]=intval($id);
		}
	}
	foreach($dels as $id){
		pdo_delete('core_plugin_permissions',array('id'=>$id));
	}
	exi('删除成功！');
}
